package org.ktjiaoyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jakarta.annotation.Resource;
import org.ktjiaoyu.mapper.PermissionMapper;
import org.ktjiaoyu.mapper.RoleMapper;
import org.ktjiaoyu.mapper.UserMapper;
import org.ktjiaoyu.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jane
 * @date 2025-02-11 14:13
 */
@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("---------MyUserDetailsService-----------");

//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username",username);
//        User user = userMapper.selectOne(queryWrapper);

        LambdaQueryWrapper<SysUser> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername,username);

        SysUser sysUser = userMapper.selectOne(queryWrapper);


        if (null==(sysUser)){
            throw new UsernameNotFoundException("用户名不存在");
        }

        //根据用户查角色 再根据角色获取权限

//        return new User(sysUser.getName(),sysUser.getPassword()//,passwordEncoder.encode("123456")
//                 , AuthorityUtils.commaSeparatedStringToAuthorityList
//                                ("L0601,L0503")  //角色,ROLE_xiaozhang
//        );

        return new SecurityUser(sysUser,getPermissions(sysUser));
    }

    public List<String> getPermissions(SysUser sysUser){
        //根据用户查询角色集合
        List<Role> roleList  = roleMapper.selectJoinList(Role.class,
                new MPJLambdaWrapper<Role>()
                        .selectAll(Role.class)
                        .innerJoin(UserRole.class,UserRole::getRoleId,Role::getId)
                        // .innerJoin(SysUser.class,SysUser::getId,UserRole::getUserId)
                        .eq(UserRole::getUserId,sysUser.getId()));

        //转成[]
        List<Integer> collect = roleList.stream().map(Role::getId).collect(Collectors.toList());

        System.out.println("collect: "+ collect);

        //根据用户角色查询权限集合
        List<Permission> permissions =permissionMapper.selectJoinList(Permission.class,
                new MPJLambdaWrapper<Permission>()
                        .selectAll(Permission.class)
                        .innerJoin(RolePermission.class,RolePermission::getPermission,Permission::getId)
                        .innerJoin(Role.class,Role::getId,RolePermission::getRoleId)
                        .in(Role::getId,collect.toArray())
        );


        System.out.println(permissions);
        //处理权限列表 list
        List<String> strList =permissions.stream().map(Permission::getPermit).collect(Collectors.toList());
        return  strList;
    }
}
